﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkStation.UI.Controls
{
    public class MotionInfo
    {
        private List<MotionShape> _shapes;
        private LineMotion _vLine;
        private LineMotion _hLine;
        private LineMotion _rLine;
        private TextMotion _hText;
        private TextMotion _vText;
        private TextMotion _rText;
        public MotionInfo(List<MotionShape> shapes)
        {
            this._shapes = shapes;
            //Update(0, 0, totalWith, totalHeight, marginLeft, marginUp, marginRight, marginDown);
        }
        public void Update(int x, int y, 
                           int totalWidth, int totalHeight, int marginLeft, int marginUp, int marginRight, int marginDown,
                           MotionCoordinate crdH, MotionCoordinate crdV, MotionCoordinate crdR)
        {
            if (_hLine == null && crdV != null && (crdV.Direction == AxisDir.Up2Down || crdV.Direction == AxisDir.Down2Up))
            {
                //增加垂直&水平指示线
                _hLine = new LineMotion();
                _hLine.Style = System.Drawing.Drawing2D.DashStyle.Dash;
                _hLine.LineWidth = 2;
                _hLine.PaintColor = Color.LightGray;
                _hLine.EnableEndCap = false;
                _shapes.Add(_hLine);
            }
            if (_vLine == null && crdH != null && (crdH.Direction == AxisDir.Left2Right || crdH.Direction == AxisDir.Right2Left))
            {
                _vLine = new LineMotion();
                _vLine.Style = System.Drawing.Drawing2D.DashStyle.Dash;
                _vLine.LineWidth = 2;
                _vLine.PaintColor = Color.LightGray;
                _vLine.EnableEndCap = false;
                _shapes.Add(_vLine);
            }
            if (_rLine == null && crdR != null && crdR.Direction == AxisDir.Circle)
            {
                _rLine = new LineMotion();
                _rLine.Style = System.Drawing.Drawing2D.DashStyle.Dash;
                _rLine.LineWidth = 2;
                _rLine.PaintColor = Color.LightGray;
                _rLine.EnableEndCap = false;
                _shapes.Add(_rLine);
            }
            if (_hLine != null && crdV != null && (crdV.Direction == AxisDir.Up2Down || crdV.Direction == AxisDir.Down2Up))
            {
                _hLine.StartPoint = new PointF(0, y);
                _hLine.EndPoint = new PointF(totalWidth + marginRight + marginLeft, y);
            }
            if (_vLine != null && crdH != null && (crdH.Direction == AxisDir.Left2Right || crdH.Direction == AxisDir.Right2Left))
            {
                _vLine.StartPoint = new PointF(x, 0);
                _vLine.EndPoint = new PointF(x, totalHeight + marginUp + marginDown);
            }
            if (_rLine != null && crdR != null && crdR.Direction == AxisDir.Circle)
            {
                _rLine.StartPoint = totalWidth < totalHeight ? new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + totalHeight / 2)) : new PointF((float)(marginLeft + totalWidth / 2), (float)(marginUp + totalHeight / 2));
                double xx = x - _rLine.StartPoint.X;
                double yy = y - _rLine.StartPoint.Y;
                double l = Math.Sqrt(xx * xx + yy * yy);
                double r = totalWidth < totalHeight ? totalWidth / 2 : totalHeight / 2;
                _rLine.EndPoint = new PointF((float)(xx * 1.3 * r / l + _rLine.StartPoint.X), (float)(yy * 1.3 * r / l + _rLine.StartPoint.Y));
            }
            if (crdH != null)
            {
                double text = 0;
                PointF pos = new PointF();
                crdH.GetActualPosition(x, y, marginLeft, marginUp, totalWidth, totalHeight, out text, out pos);
                if(_hText == null)
                {
                    _hText = new TextMotion();
                    _hText.PaintColor = Color.BlueViolet;
                    _hText.FontName = "黑体";
                    _hText.FontSize = 8.5f;
                    _shapes.Add(_hText);
                }
                _hText.Text = text.ToString("0.00") + "mm";
                _hText.StartPoint = pos;
            }
            if(crdV != null)
            {
                double text = 0;
                PointF pos = new PointF();
                crdV.GetActualPosition(x, y, marginLeft, marginUp, totalWidth, totalHeight, out text, out pos);
                if (_vText == null)
                {
                    _vText = new TextMotion();
                    _vText.PaintColor = Color.BlueViolet;
                    _vText.FontName = "黑体";
                    _vText.FontSize = 8.5f;
                    _shapes.Add(_vText);
                }
                _vText.Text = text.ToString("0.00") + "mm";
                _vText.StartPoint = pos;
            }
            if(crdR != null)
            {
                double text = 0;
                PointF pos = new PointF();
                crdR.GetActualPosition(x, y, marginLeft, marginUp, totalWidth, totalHeight, out text, out pos);
                if (_rText == null)
                {
                    _rText = new TextMotion();
                    _rText.PaintColor = Color.BlueViolet;
                    _rText.FontName = "黑体";
                    _rText.FontSize = 8.5f;
                    _shapes.Add(_rText);
                }
                _rText.Text = text.ToString("0.00") + "°";
                _rText.StartPoint = pos;
            }
        }
        public void Clear()
        {
            _vLine = new LineMotion();
            _vLine = null;
            _hLine = new LineMotion();
            _hLine = null;
            _hText = new TextMotion();
            _hText = null;
            _vText = new TextMotion();
            _vText = null;
        }
    }
}
